import { create } from 'zustand';
import {
  getChapterVideoByIdApi,
  getCourseInfoByIdApi,
} from '../apis/courseApi';
import { ChapterVideoItem, courseOutlineObject } from '../models/courseModel';

interface CoruseState {
  bears: number;
  increase: (by: number) => void;
  // 课程基本信息
  courseInfo: courseOutlineObject;
  // 根据课程id查询课程基本信息
  getCourseInfoByIdApi: (id: number) => Promise<void>;
  // 课程列表
  courseList: ChapterVideoItem[];
  // 根据课程id查询课程大纲列表
  getChapterVideoByIdApi: (id: number) => Promise<void>;
}

export const useCourseStore = create<CoruseState>()(set => ({
  bears: 0,
  increase: by => set(state => ({ bears: state.bears + by })),
  courseInfo: {},
  // 根据课程id查询课程基本信息
  getCourseInfoByIdApi: async (id: number) => {
    const res = await getCourseInfoByIdApi(id);
    set({ courseInfo: res.data });
    // console.log(res);
  },
  courseList: [],
  // 根据课程id查询课程大纲列表
  getChapterVideoByIdApi: async (id: number) => {
    const res = await getChapterVideoByIdApi(id);
    // console.log(res);
    set({ courseList: res.data.allChapterVideo });
  },
}));
